package com.robot.base.utils

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.engine.CompressFileEngine
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.utils.DateUtils
import com.robot.base.util.Permission.PermissionInterceptor
import com.robot.base.util.image.*
import java.io.File

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2022/1/27 15:39
 */
object ChoiceImgHelper {

    fun choice(
        activity: Activity, selectNum: Int, croup: Boolean = false,
        resultFunc: (result: List<LocalMedia?>) -> Unit,
        cancelFunc: () -> Unit
    ) {
        XXPermissions.with(activity)
            .permission(Permission.MANAGE_EXTERNAL_STORAGE)
            .interceptor(PermissionInterceptor())
            .request { permissions, allGranted ->
                choiceImg(activity, selectNum, croup, resultFunc, cancelFunc)
            }
    }

    private fun choiceImg(
        activity: Activity, selectNum: Int, croup: Boolean = false,
        resultFunc: (result: List<LocalMedia?>) -> Unit,
        cancelFunc: () -> Unit
    ) {


        //TODO 相册调整
        val picture = PictureSelector.create(activity)
            .openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .setMaxSelectNum(selectNum)
            .setCompressEngine(ImageFileCompressEngine())
//            .selectionMode(PictureConfig.MULTIPLE)
//            .maxSelectNum(6)
//            .isCompleteSelectRelativeTop
//            .minimumCompressSize(100)

        picture.forResult(object : OnResultCallbackListener<LocalMedia> {
            override fun onCancel() {
                cancelFunc.invoke()
            }

            override fun onResult(result: ArrayList<LocalMedia?>?) {
                resultFunc(result!!)
            }
        })
    }

    fun choiceOpenCamera(
        activity: Activity,
        resultFunc: (result: List<LocalMedia?>) -> Unit?,
        cancelFunc: () -> Unit?
    ) {
        XXPermissions.with(activity)
            .permission(Permission.CAMERA)
            .interceptor(PermissionInterceptor())
            .request { permissions, allGranted ->
                //TODO 相机调整
                val picture = PictureSelector.create(activity)
                    .openCamera(SelectMimeType.ofImage())
                    .setCompressEngine(ImageFileCompressEngine())

                picture.forResult(object : OnResultCallbackListener<LocalMedia> {
                    override fun onCancel() {
                        cancelFunc
                    }

                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                        resultFunc(result!!)
                    }

                })
            }

    }
//    fun ChoiceImg(
//        activity: Fragment,
//        resultFunc: (result: List<LocalMedia?>) -> Unit,
//        cancelFunc: () -> Unit?
//    ) {
//        PictureSelector.create(activity)
//            .openGallery(PictureMimeType.ofImage())
//            .imageEngine(GlideEngine.createGlideEngine())
//            .isCompress(true)
//            .selectionMode(PictureConfig.SINGLE)
//            .minimumCompressSize(100)
//            .forResult(object : OnResultCallbackListener<LocalMedia?> {
//                override fun onResult(result: List<LocalMedia?>) {
//                    resultFunc(result)
//                }
//
//                override fun onCancel() {
//                    cancelFunc
//                }
//            })
//    }

    /**
     * 自定义压缩
     */
    private class ImageFileCompressEngine : CompressFileEngine {
        override fun onStartCompress(
            context: Context,
            source: ArrayList<Uri>,
            call: OnKeyValueResultCallbackListener
        ) {
            Luban.with(context).load(source).ignoreBy(100)
                .setRenameListener { filePath ->
                    val indexOf = filePath.lastIndexOf(".")
                    val postfix = if (indexOf != -1) filePath.substring(indexOf) else ".jpg"
                    DateUtils.getCreateFileName("CMP_") + postfix
                }.setCompressListener(object : OnNewCompressListener {
                    override fun onStart() {}
                    override fun onSuccess(source: String?, compressFile: File) {
                        if (call != null) {
                            call.onCallback(source, compressFile.absolutePath)
                        }
                    }

                    override fun onError(source: String?, e: Throwable?) {
                        if (call != null) {
                            call.onCallback(source, null)
                        }
                    }
                }).launch()
        }
    }
}